//
//  MainVideoLiveAlertBaseView.m
//  v8090Video
//
//  Created by 陈建 on 2018/9/3.
//  Copyright © 2018年 BlueWind. All rights reserved.
//

#import "MainVideoLiveAlertBaseView.h"


@interface MainVideoLiveAlertBaseView ()

// 背景视图
@property (nonatomic, strong) UIView *backgroundView;


@end

@implementation MainVideoLiveAlertBaseView


- (void)setAlertViewHeight:(CGFloat)alertViewHeight{
    _alertViewHeight = alertViewHeight;
    self.alertView.height = alertViewHeight + Height_TabBar_Space;
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
       
        [self setupUI];

    }
    return self;
}



- (void)setupUI{
    [self addSubview:self.backgroundView];
    [self addSubview:self.alertView];
}

#pragma mark - 弹出视图方法
- (void)showWithAnimation:(BOOL)animation {

    //1. 获取当前应用的主窗口
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];

    if (animation) {
        // 动画前初始位置
        CGRect rect = self.alertView.frame;
        rect.origin.y = self.height;
        self.alertView.frame = rect;
        
        // 浮现动画
        [UIView animateWithDuration:0.3 animations:^{
            CGRect rect = self.alertView.frame;
            rect.origin.y -= _alertViewHeight;
            self.alertView.frame = rect;

        }];
    }

}

#pragma mark - 关闭视图方法
- (void)dismissWithAnimation:(BOOL)animation {
    // 关闭动画
    if (animation) {
        [UIView animateWithDuration:0.2 animations:^{
            CGRect rect = self.alertView.frame;
            rect.origin.y += _alertViewHeight;
            self.alertView.frame = rect;
            
            self.backgroundView.alpha = 0;
        } completion:^(BOOL finished) {
            if (self.WillBackViewDismiss) {
                self.WillBackViewDismiss();
            }
            [self.alertView removeFromSuperview];
            [self.backgroundView removeFromSuperview];
            
            [self removeFromSuperview];
            
            [self removeAndAlertViewSubViews];
            self.alertView = nil;
            self.backgroundView = nil;
        }];
    }else{
        if (self.WillBackViewDismiss) {
            self.WillBackViewDismiss();
        }
        [self.alertView removeFromSuperview];
        [self.backgroundView removeFromSuperview];
        
        [self removeFromSuperview];
        
        [self removeAndAlertViewSubViews];
        self.alertView = nil;
        self.backgroundView = nil;
    }
   
}

-(void)removeAndAlertViewSubViews{
    
}


#pragma mark - 背景遮罩图层
- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc]initWithFrame:self.bounds];
//        _backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2f];
        _backgroundView.backgroundColor = [UIColor clearColor];
        _backgroundView.userInteractionEnabled = YES;
        UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapBackgroundView:)];
        [_backgroundView addGestureRecognizer:myTap];
    }
    return _backgroundView;
}
#pragma mark - 点击背景遮罩图层事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    [self dismissWithAnimation:YES];
}

#pragma mark - 弹出视图
- (UIView *)alertView {
    if (!_alertView) {
        _alertView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT- _alertViewHeight, SCREEN_WIDTH,_alertViewHeight)];
        _alertView.backgroundColor = [UIColor clearColor];
    }
    return _alertView;
}

@end
